package com.sooth.common.dream.sequence;

import com.dream.system.table.ColumnInfo;
import com.dream.template.sequence.AbstractSequence;
import com.sooth.common.core.exception.ServiceException;
import com.sooth.common.core.lang.SnowFlake;

public class SnowFlakeSequence extends AbstractSequence {
    private SnowFlake snowFlake;

    public SnowFlakeSequence(SnowFlake snowFlake) {
        this.snowFlake = snowFlake;
    }

    @Override
    protected Object sequence(ColumnInfo columnInfo) {
        Class<?> type = columnInfo.getField().getType();
        Long nextId = snowFlake.next();
        if (type == Long.class) {
            return nextId;
        } else if (type == String.class) {
            return Long.toHexString(nextId);
        } else if (type == Integer.class) {
            return nextId.intValue();
        }
        throw new ServiceException("不支持的主键类型：" + type.getName());
    }
}
